#include "./minifontwnd.h"
#include "./fonttypetest.h"

using namespace MiniUI;
using namespace MiniUI::Type;

MiniFontWnd::MiniFontWnd()
{
    m_pWinView = WinView::CreateInstance();
    m_pWinView->SetWinBorder(MiniRect(0, 0, 0, 0));
    m_pWinView->OpenWindow(MiniRect(100, 100, 689, 546));

    Image* pBkg = Image::CreateInstance();
    pBkg->SetValue("wnd_bkg.png");
    pBkg->SetAnchors(UIObject::ANCHOR_LEFT | UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT | UIObject::ANCHOR_BOTTOM);
    pBkg->SetMargin(MiniRect(0, 0, 0, 0));
    pBkg->SetTransparent(true);
    m_pWinView->AddChild(pBkg);
    pBkg->Release();

    View* pView = View::CreateInstance();
    m_pWinView->AddChild(pView);
    pView->SetBackgroundColor(MiniARGB(255, 100, 100, 100));
    pView->SetPos(MiniPoint(18, 255)); //195 //255
    pView->SetSize(MiniSize(20, 20));
    pView->Release();

    Button* pBtnClose = Button::CreateInstance();
    pBtnClose->OnClick += EventObject<OnClickEventFunc>(this, &MiniFontWnd::OnBtnCloseClick);
    pBtnClose->Init("btnclose_normal.png", "btnclose_hover.png", "btnclose_down.png");
    m_pWinView->AddChild(pBtnClose);
    pBtnClose->SetAnchors(UIObject::ANCHOR_TOP | UIObject::ANCHOR_RIGHT);
    pBtnClose->SetMargin(MiniRect(0, 1, 3, 0));
    pBtnClose->Release();

    Button* pBtnNothing = Button::CreateInstance();
    pBtnNothing->OnClick += EventObject<OnClickEventFunc>(this, &MiniFontWnd::OnBtnNothingClick);
    pBtnNothing->Init("mybtn_normal.png", "mybtn_hover.png", "mybtn_down.png");
    pBtnNothing->SetText("nothing");
    m_pWinView->AddChild(pBtnNothing);
    pBtnNothing->SetPos(MiniPoint(20, 50));
    pBtnNothing->SetSize(MiniSize(66, 23));
    pBtnNothing->Release();

    m_pImgNothing = Image::CreateInstance();
    m_pWinView->AddChild(m_pImgNothing);
    m_pImgNothing->SetPos(MiniPoint(20, 80));

    Button* pBtnClearType = Button::CreateInstance();
    pBtnClearType->OnClick += EventObject<OnClickEventFunc>(this, &MiniFontWnd::OnBtnClearTypeClick);
    pBtnClearType->Init("mybtn_normal.png", "mybtn_hover.png", "mybtn_down.png");
    pBtnClearType->SetText("cleartype");
    m_pWinView->AddChild(pBtnClearType);
    pBtnClearType->SetPos(MiniPoint(20, 110));
    pBtnClearType->SetSize(MiniSize(66, 23));
    pBtnClearType->Release();

    m_pImgClearType = Image::CreateInstance();
    m_pWinView->AddChild(m_pImgClearType);
    m_pImgClearType->SetPos(MiniPoint(20, 140));

    Button* pBtnClearType2 = Button::CreateInstance();
    pBtnClearType2->OnClick += EventObject<OnClickEventFunc>(this, &MiniFontWnd::OnBtnClearType2Click);
    pBtnClearType2->Init("mybtn_normal.png", "mybtn_hover.png", "mybtn_down.png");
    pBtnClearType2->SetText("cleartype2");
    m_pWinView->AddChild(pBtnClearType2);
    pBtnClearType2->SetPos(MiniPoint(20, 170));
    pBtnClearType2->SetSize(MiniSize(66, 23));
    pBtnClearType2->Release();

    m_pImgClearType2 = Image::CreateInstance();
    m_pWinView->AddChild(m_pImgClearType2);
    m_pImgClearType2->SetPos(MiniPoint(20, 200));

    Button* pBtnAntialias = Button::CreateInstance();
    pBtnAntialias->OnClick += EventObject<OnClickEventFunc>(this, &MiniFontWnd::OnBtnAntialiasClick);
    pBtnAntialias->Init("mybtn_normal.png", "mybtn_hover.png", "mybtn_down.png");
    pBtnAntialias->SetText("antialias");
    m_pWinView->AddChild(pBtnAntialias);
    pBtnAntialias->SetPos(MiniPoint(20, 230));
    pBtnAntialias->SetSize(MiniSize(66, 23));
    pBtnAntialias->Release();

    m_pImgAntialias = Image::CreateInstance();
    m_pWinView->AddChild(m_pImgAntialias);
    m_pImgAntialias->SetPos(MiniPoint(20, 260));

    m_pImgGrid = Image::CreateInstance();
    m_pWinView->AddChild(m_pImgGrid);
    m_pImgGrid->SetPos(MiniPoint(120, 50));
    m_pImgGrid->SetSize(MiniSize(450, 380));

    m_pGridBitmap = Bitmap::CreateInstance();
}

MiniFontWnd::~MiniFontWnd()
{
    m_pWinView->Release();
    m_pImgNothing->Release();
    m_pImgClearType->Release();
    m_pImgClearType2->Release();
    m_pImgAntialias->Release();
    m_pImgGrid->Release();
    m_pGridBitmap->Release();
}

void MiniFontWnd::OnBtnCloseClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    ::PostQuitMessage(0);
}

void MiniFontWnd::OnBtnNothingClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    FontTypeTest test;
    FontTypeTest::FontBuffer fontBuffer = test.GetGlyphBuffer(L'm', FontTypeTest::FontMode_Nothing);
    Bitmap* pBitmap = Bitmap::CreateInstance();
    pBitmap->Rebuild(MiniSize(fontBuffer.width, fontBuffer.height), fontBuffer.buf);
    m_pImgNothing->SetBitmap(pBitmap);
    m_pImgNothing->SetSize(MiniSize(fontBuffer.width, fontBuffer.height));
    pBitmap->Release();

    GridShow(fontBuffer.width, fontBuffer.height, (unsigned int*)fontBuffer.buf);
}

void MiniFontWnd::OnBtnClearTypeClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    FontTypeTest test;
    FontTypeTest::FontBuffer fontBuffer = test.GetGlyphBuffer(L'm', FontTypeTest::FontMode_ClearType);
    Bitmap* pBitmap = Bitmap::CreateInstance();
    pBitmap->Rebuild(MiniSize(fontBuffer.width, fontBuffer.height), fontBuffer.buf);
    m_pImgClearType->SetBitmap(pBitmap);
    m_pImgClearType->SetSize(MiniSize(fontBuffer.width, fontBuffer.height));
    pBitmap->Release();

    GridShow(fontBuffer.width, fontBuffer.height, (unsigned int*)fontBuffer.buf);
}

void MiniFontWnd::OnBtnClearType2Click(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    FontTypeTest test;
    FontTypeTest::FontBuffer fontBuffer = test.GetGlyphBuffer(L'm', FontTypeTest::FontMode_ClearType2);
    Bitmap* pBitmap = Bitmap::CreateInstance();
    pBitmap->Rebuild(MiniSize(fontBuffer.width, fontBuffer.height), fontBuffer.buf);
    m_pImgClearType2->SetBitmap(pBitmap);
    m_pImgClearType2->SetSize(MiniSize(fontBuffer.width, fontBuffer.height));
    pBitmap->Release();

    GridShow(fontBuffer.width, fontBuffer.height, (unsigned int*)fontBuffer.buf);
}

void MiniFontWnd::OnBtnAntialiasClick(const UIObject* sender, const Type::MiniPoint& pt, MouseButton button, uint keyState)
{
    FontTypeTest test;
    FontTypeTest::FontBuffer fontBuffer = test.GetGlyphBuffer(L'm', FontTypeTest::FontMode_Antialias);
    Bitmap* pBitmap = Bitmap::CreateInstance();
    pBitmap->Rebuild(MiniSize(fontBuffer.width, fontBuffer.height), fontBuffer.buf);
    m_pImgAntialias->SetBitmap(pBitmap);
    m_pImgAntialias->SetSize(MiniSize(fontBuffer.width, fontBuffer.height));
    pBitmap->Release();

    GridShow(fontBuffer.width, fontBuffer.height, (unsigned int*)fontBuffer.buf);
}

void MiniFontWnd::GridShow(int width, int height, unsigned int* src)
{
    MiniSize sizeImg;
    m_pImgGrid->GetSize(sizeImg);

    m_pGridBitmap->Rebuild(sizeImg);
    void* dst = m_pGridBitmap->GetBytes();
    memset(dst, 0, sizeImg.cx * sizeImg.cy * 4);

    MiniSize sizeGrid(sizeImg.cx / width, sizeImg.cy / height);
    for (int j = 0; j < height; ++j)
    {
        for (int i = 0; i < width; ++i)
        {
            unsigned int* buf = (unsigned int*)dst;
            buf += j * sizeGrid.cy * sizeImg.cx + i * sizeGrid.cx;
            for (int y = 0; y < sizeGrid.cy; ++y)
            {
                for (int x = 0; x < sizeGrid.cx; ++x)
                {
                    buf[x] = *src;
                }
                buf += sizeImg.cx;
            }
            src++;
        }
    }

    m_pImgGrid->SetBitmap(m_pGridBitmap);
    m_pImgGrid->Update();
}